package com.blueocean_health.care.management.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.blueocean_health.care.management.domain.base.OrderServiceNurseInfo;
import com.blueocean_health.care.management.domain.ordervo.OrderServiceNurseInfoVo;
import com.blueocean_health.care.management.util.MyMapper;

@Mapper
public interface OrderServiceNurseInfoMapper extends MyMapper<OrderServiceNurseInfo> {
	@Select("SELECT COUNT(*) FROM order_service_nurse_info b WHERE " +
			"b.VERSION = " +
			"(SELECT " +
			"MAX(VERSION) " +
			"FROM " +
			"order_service_nurse_info "+
			"WHERE b.order_id = order_id AND cancel = 0 "
			+ "AND service_type != 4 ) "
			+ "AND b.worker_id=#{workerId}")
	int getMaxVersionWorkerIdNumber(@Param("workerId")String workerId);
	
	
	@Select("SELECT *FROM `order_service_nurse_info` b WHERE " +
            "b.VERSION = " +
            "(SELECT " +
            "MAX(VERSION) " +
            "FROM " +
            "order_service_nurse_info " +
            "WHERE b.order_id = order_id AND cancel = 0 AND service_type = 4)")
	List<OrderServiceNurseInfo> getAllMaxVersionGroupOrder();

	/**
	 * 获取最大版本
	 * @return
	 */
//	List<OrderServiceNurseInfo> getAllMaxVersionEndtime();
	@Select("  SELECT t1.*,t2.start_time  AS refundStart,t2.end_time  AS refundEnd" +
            "  FROM" +
            "  (" +
            "  SELECT * FROM `order_service_nurse_info` os WHERE VERSION = (SELECT MAX(VERSION) FROM order_service_nurse_info WHERE os.order_id = order_id) " +
            "  AND cancel = 0 AND `order_type` != 3  AND `service_type` != 4" +
            "  ) t1" +
            "  LEFT JOIN" +
            "  (" +
            "  SELECT * FROM `order_service_nurse_info` os WHERE VERSION = (SELECT MAX(VERSION) FROM order_service_nurse_info WHERE os.order_id = order_id) " +
            "  AND cancel = 0 AND `order_type` = 3  AND `service_type` != 4" +
            "  ) t2 ON  t2.refund_service_id = t1.service_id" +
            "  LEFT JOIN nurse_worker_info t3 ON t3.`worker_id` = t1.worker_id" +
            "  WHERE" +
            "  t3.`hospital` =#{hospital}")
	List<OrderServiceNurseInfoVo> getAllMaxVersionEndtimeNew(String hospital);
	
	/**
	 * 根据开始时间和结束时间获取在"工作"状态的护工
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	List<OrderServiceNurseInfoVo> selectNurseByWorking(@Param("hospital")String hospital,@Param("startTime")String startTime, @Param("endTime")String endTime);

}